//
//  ViewController.m
//  BT_1
//
//  Created by 楼楚 on 16/1/20.
//  Copyright © 2016年 Chu Lou. All rights reserved.
//

#import "ViewController.h"
#include <GameKit/GameKit.h>
@interface ViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate,GKPeerPickerControllerDelegate>
- (IBAction)connect;

- (IBAction)selectPic;

- (IBAction)send;

@property (weak, nonatomic) IBOutlet UIImageView *customView;

@property (strong,nonatomic) GKSession *session;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)connect
{
//    1.创建选择其他蓝牙设备的控制器
    GKPeerPickerController *pPC = [[GKPeerPickerController alloc]init];
//    2.成为该控制器的代理
    pPC.delegate = self;
//    3.显示蓝牙控制器
    [pPC show];

    
}
#pragma mark - 蓝牙控制器代理方法
 //    4.实现代理方法


/*当蓝牙设备连接成功 就会调用
 *@param picker 触发时间的控制器
 *@param peerID 连接蓝牙设备的ID
 *@param session 连接蓝牙的会话
 */
- (void)peerPickerController:(GKPeerPickerController *)picker didConnectPeer:(NSString *)peerID toSession:(GKSession *)session
{
    NSLog(@"%@",peerID);
    
    self.session = session;
    
//  设置监听接收传递过来的数据
    
 /*
  *DataReceiveHandler:谁来处理接收到的数据
  *withContext:传递数据
  */
    [self.session setDataReceiveHandler:self withContext :nil];
    
    
    
    [picker dismiss];
}
/*
     *接收到其他设备传递过来的数据就会调用
     *
     * @param data 传递过来的数据
     * @param peer 传递数据设备的ID
     * @param session 会话
     * @param context 注册监听时传递的数据
     */

- (void) receiveData:(NSData *)data fromPeer:(NSString *) peer inSession:(GKSession *)session context:(void *)context
{
      NSLog(@"%s",__func__);
    UIImage *img = [UIImage imageWithData:data];
    self.customView.image = img;
}






- (void)peerPickerControllerDidCancel:(GKPeerPickerController *)picker
{
  
}

- (IBAction)selectPic {
    //1.创建图片选择器
    UIImagePickerController *imgPicker = [[UIImagePickerController alloc]init];
    
    //2.判断图库是否可用打开
    
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]) {
        //3.设置打开图库的类型
        
        imgPicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
        imgPicker.delegate = self;
        //4.打开图片选择器
        [self presentViewController:imgPicker animated:YES completion:nil];
    }

}

#pragma mark - 图片选择代理
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
//    NSLog(@"%@", info);
    self.customView.image = info[UIImagePickerControllerOriginalImage];
    [picker dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)send

{
//    利用session发送图片
    
    UIImage *img = self.customView.image;
    NSData *data = UIImagePNGRepresentation(img);
    
//    GKSendDataReliable 数据安全的发送模式，慢
//    GKSendDataUnreliable 数据不安全的发送模式，快
//    error:是否监听发送错误
    
    [self.session sendDataToAllPeers:data withDataMode:GKSendDataReliable error:nil];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
